load('application');

before(loadInbox, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
    this.title = 'New inbox';
    this.inbox = new Inbox;
    render();
});

action(function create() {
    Inbox.create(req.body.Inbox, function (err, inbox) {
        if (err) {
            flash('error', 'Inbox can not be created');
            render('new', {
                inbox: inbox,
                title: 'New inbox'
            });
        } else {
            flash('info', 'Inbox created');
            redirect(path_to.inboxes());
        }
    });
});

action(function index() {
    this.title = 'Inboxs index';
    Inbox.all(function (err, inboxes) {
        render({
            inboxes: inboxes
        });
    });
});

action(function show() {
    this.title = 'Inbox show';
    render();
});

action(function edit() {
    this.title = 'Inbox edit';
    render();
});

action(function update() {
    this.inbox.updateAttributes(body.Inbox, function (err) {
        if (!err) {
            flash('info', 'Inbox updated');
            redirect(path_to.Inbox(this.Inbox));
        } else {
            flash('error', 'Inbox can not be updated');
            this.title = 'Edit inbox details';
            render('edit');
        }
    }.bind(this));
});

action(function destroy() {
    this.inbox.destroy(function (error) {
        if (error) {
            flash('error', 'Can not destroy inbox');
        } else {
            flash('info', 'Inbox successfully removed');
        }
        send("'" + path_to.inboxes() + "'");
    });
});

function loadInbox() {
    Inbox.find(params.id, function (err, inbox) {
        if (err || !inbox) {
            redirect(path_to.inboxes());
        } else {
            this.inbox = inbox;
            next();
        }
    }.bind(this));
}
